import base64

from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By

from Python.Python.utils import slider_match
# from slider_match import calculate_slider_offset
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options

from Python.Python.utils.element_utils import is_element_exist

#滑块破解

# 替换之后代码
# 2. 初始化 Options 并配置浏览器参数（可选，根据需求添加）
print("111")
options = Options()  # 创建 Options 实例
print("222")
# 示例：添加常见配置（可选）
#options.add_argument("--start-maximized")  # 最大化窗口
options.add_argument("--remote-allow-origins=*")
print("333")
#driver_path = "D:\chromedriver-win64\chromedriver.exe" # 下载好的驱动文件夹，解压之后里面会有一个**driver.exe，这里填这个文件的位置
print("444")
#driver = webdriver.Chrome(service=Service(driver_path),options=options)

#driver = webdriver.Chrome()#新版selenium，无需配置driver
chrome_driver_path= "D:\\chromedriver-win64\\chromedriver.exe"
# 这里通过service=Service(指定浏览器驱动路径)
driver = webdriver.Chrome(service=Service(chrome_driver_path),options=options)
#driver.maximize_window()#窗口最大化
#打开网页
driver.get("https://pre.yaoyi.net/#/login")


#隐式等待，提高稳定性
driver.implicitly_wait(10)
#input("请输入验证码")#这个是我用来控制程序不要关闭的~当时忘记怎么等待了~~~



driver.find_element("xpath",'//*[@id="app"]/div[1]/div[1]/div[2]/div/div[1]/form/div[1]/div/div/input').send_keys("18233490002")
driver.find_element("xpath",'//*[@id="app"]/div[1]/div[1]/div[2]/div/div[1]/form/div[3]/div/div/input').send_keys("490002")
driver.find_element('xpath','//*[@id="app"]/div[1]/div[1]/div[2]/div/div[1]/form/div[5]/div/button').click()


# big_ele = driver.find_element("xpath",'/html/body/div[3]/div/div[2]/div/div[1]/div/img')
# small_ele = driver.find_element("xpath",'/html/body/div[3]/div/div[2]/div/div[2]/div/div/div/img')
# big_src = big_ele.get_attribute("src")
# small_src = small_ele.get_attribute("src")
#注意：属性是什么

#可以打出来看一下
#print(big_src)
#逗号前边是前端使用base64的标志，后边才是标准的base64编码值


#获取标准的base64编码
# # big_base64 = big_src.split(",")[1]
# big_src = big_src[big_src.find(',')+1:]#
# small_src = small_src[small_src.find(',')+1:]


# #写入文件（写入二进制）
# #把base64转成二进制文件
# with open('./big.png',mode='wb') as f :#python中的垃圾回收机制
#     #先把base64转换成二进制，然后写入文件
#     f.write(base64.b64decode(big_src))
#
#
# with open('./small.png',mode='wb') as f :#python中的垃圾回收机制
#     #先把base64转换成二进制，然后写入文件
#     f.write(base64.b64decode(small_src))
#
# # slider = driver.find_element_by_xpath("/html/body/div[3]/div/div[2]/div/div[2]/div/div/div/img")
# slider = driver.find_element("xpath","/html/body/div[3]/div/div[2]/div/div[2]/div/div/div/img")

# distance = calculate_slider_offset("./big.png","./small.png")*(661/617)
# ActionChains(driver).drag_and_drop_by_offset(slider, distance, 0).perform()
#视频中的代码：
#action = ActionChains(driver)
#按住小的滑块，鼠标沿X轴拖动需要的距离（res）,松开鼠标，使前面操作生效。(这里的注释相当恰当清醒好理解，看一看)
#action.click_and_hold(small_ele).move_by_offset(res,0).release().perform()
#


#driver.find_element("xpath","/html/body/div[3]/div/div[2]/div/div[2]/div/div").click()


# driver.find_element("xpath","/html/body/div[3]/div/div[2]/div/div[2]/div/div").click()

driver.implicitly_wait(10)#隐式等待需要等待多次嘛？
#输入验证码
# print("输入验证码，点击登录")
# driver.find_element("xpath","/html/body/div[5]/div/div[2]/form/div[2]/div/button/span").click()
# driver.find_element("xpath","/html/body/div[5]/div/div[2]/form/div[2]/input").send_keys("5657")
# driver.find_element("xpath","/html/body/div[5]/div/div[3]/span/button").click()
#is_element_exist((By.XPATH,''''''))
# driver.find_element("xpath","/html/body/div[5]/div/div[2]/form/div[2]/div/button/span").click()
# driver.find_element("xpath","/html/body/div[5]/div/div[2]/form/div[2]/input").send_keys("5657")
# driver.find_element("xpath","/html/body/div[5]/div/div[3]/span/button").click()

while True:
    big_ele = driver.find_element("xpath", '/html/body/div[3]/div/div[2]/div/div[1]/div/img')
    small_ele = driver.find_element("xpath", '/html/body/div[3]/div/div[2]/div/div[2]/div/div/div/img')
    big_src = big_ele.get_attribute("src")
    small_src = small_ele.get_attribute("src")

    big_src = big_src[big_src.find(',') + 1:]  #
    small_src = small_src[small_src.find(',') + 1:]

    # 写入文件（写入二进制）
    # 把base64转成二进制文件
    with open('./big.png', mode='wb') as f:  # python中的垃圾回收机制
        # 先把base64转换成二进制，然后写入文件
        f.write(base64.b64decode(big_src))

    with open('./small.png', mode='wb') as f:  # python中的垃圾回收机制
        # 先把base64转换成二进制，然后写入文件
        f.write(base64.b64decode(small_src))

    slider = driver.find_element("By.xpath", "/html/body/div[3]/div/div[2]/div/div[2]/div/div/div/img")

    distance = slider_match.calculate_slider_offset("./big.png", "./small.png") * (661 / 617)
    ActionChains(driver).drag_and_drop_by_offset(slider, distance, 0).perform()

    if is_element_exist((By.XPATH,'''/html/body/div[5]/div/div[2]/form/div[2]/div/button/span''')):
        print("滑块认证成功")
        break

    print("滑块认证失败")


#点击【任务管理】-》【个人积分包管理】-》
driver.find_element("xpath",'//*[@id="app"]/div[1]/div[2]/div[1]/div/div[2]/div[1]/div/ul/div/li[6]/div/span').click()
driver.find_element("xpath",'//*[@id="app"]/div[1]/div[2]/div[1]/div/div[2]/div[1]/div/ul/div/li[6]/ul/li[4]/span').click()
print("点击【任务管理】-》【个人积分包管理】-》——完成")

#点击“新增”
driver.find_element("xpath",'//*[@id="app"]/div[1]/div[2]/div[2]/div[2]/div[1]/div/div/div[2]/div/div/div[1]/div/div/div[1]/div/button[1]/span').click()
print("点击“新增”——完成")


#选择“上游服务企业”
driver.find_element("xpath",'/html/body/div[5]/div/div[2]/div/form/div[2]/div/div/div/div/div/span/span/i').click()
driver.find_element("xpath","/html/body/div[7]/div[1]/div[1]/ul/li[2]").click()
print("选择“上游服务企业”——完成")


#选择“上游积分包”
driver.find_element("xpath",'/html/body/div[5]/div/div[2]/div/form/div[3]/div[1]/div/div/div/div/span/span/i').click()
driver.find_element('xpath','/html/body/div[8]/div[1]/div[1]/ul/li[7]').click()

#选择“接包对象类型”
driver.find_element('xpath','/html/body/div[5]/div/div[2]/div/form/div[5]/div[1]/div/div/div/div/span/span/i').click()
print("下拉框——点击完成")
# driver.find_element('xpath','''/html/body/div[9]/div[1]/div[1]/ul/li[2]''').click()
# print("选择兼职人员——完成")
# print("选择“接包对象类型”——完成")
# 
# #填写积分包名称
# driver.find_element('xpath','''/html/body/div[5]/div/div[2]/div/form/div[9]/div/div[1]/div[3]/table/tbody/tr/td[2]/div/div/input''').send_keys("SSS1")
# 
# 
# 
# #填写积分包值
# driver.find_element('xpath','''/html/body/div[5]/div/div[2]/div/form/div[9]/div/div[1]/div[3]/table/tbody/tr/td[3]/div/div/input[1]''').send_keys("3")
# 
# 
# 
# 
# driver.find_element('xpath','''/html/body/div[5]/div/div[3]/span/button[2]/span''').click()


input()




#time.sleep(5)#等待5秒再关闭退出（方便观察运行效果）